import * as HttpServices from "@/utils/HttpServices";
import { getLoginPayload } from "@/utils/server";
import store from '@/store/index';

export default {
  moduleName: "menuManage",
  namespaced: true,
  state: {
    treeData:[],
  },
  mutations:{
    SET_TREE_DATA(state,data){
        state.treeData = data
    }
  },
  actions:{
    initData({state,dispatch,commit},params){
        return new Promise((resolve,reject)=>{
            const payload = getLoginPayload("fin007001", params);
            state.logining = true;
            HttpServices.startApiReq(payload).then(res=>{
                if(res.msgcode == 1){
                    commit("SET_TREE_DATA",[res.resultdata.menu])
                    resolve(res.resultdata)
                }else{
                    reject(res.message)
                }
                state.logining = false
            })
        })
    },
    getSelectData({state,dispatch,commit},params){
        return new Promise((resolve,reject)=>{
            const payload = getLoginPayload("fin007002", params);
            state.logining = true;
            HttpServices.startApiReq(payload).then(res=>{
                if(res.msgcode == 1){
                    resolve(res.resultdata)
                }else{
                    reject(res.message)
                }
                state.logining = false
            })
        })
    },
    saveData({state,dispatch,commit},params){
        return new Promise((resolve,reject)=>{
            const payload = getLoginPayload("fin007003", params);
            state.logining = true;
            HttpServices.startApiReq(payload).then(res=>{
                if(res.msgcode == 1){
                    store.dispatch('admin/menu/updateRemoteMenu')
                    dispatch('initData')
                    resolve(res.resultdata)
                }else{
                    reject(res.message)
                }
                state.logining = false
            })
        })
    },
    deleteMenu({state,dispatch,commit},params){
        return new Promise((resolve,reject)=>{
            const payload = getLoginPayload("fin007005", params);
            state.logining = true;
            HttpServices.startApiReq(payload).then(res=>{
                if(res.msgcode == 1){
                    store.dispatch('admin/menu/updateRemoteMenu')
                    dispatch('initData')
                    resolve(res.resultdata)
                }else{
                    reject(res.message)
                }
                state.logining = false
            })
        })
    }
  },
}